package www.rlicker.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import www.rlicker.model.Feedback;
import www.rlicker.model.GroupResource;
import www.rlicker.model.SysRole;
import www.rlicker.mybatis.Criteria;
import www.rlicker.mybatis.PageResult;
import www.rlicker.service.IFeedbackClassService;
import www.rlicker.service.IFeedbackService;
import www.rlicker.service.IGroupResourceService;
import www.rlicker.service.ISysRoleService;
import www.rlicker.service.impl.SendNotificationService;
import www.rlicker.utils.BaseController;
import www.rlicker.utils.JsonResult;
import www.rlicker.utils.StringUtil;

import com.alibaba.fastjson.JSONArray;

@Controller  
@RequestMapping("/feedback")
public class FeedbackController extends BaseController{
	
	@Autowired
	private IFeedbackService service;
	
	@Autowired
	private IFeedbackClassService feedbackClassService;
	
	@Autowired
	private IGroupResourceService groupResourceService;
	
	@Autowired
	private ISysRoleService sysRoleService;
	
	/**
	 * 跳转建议列表页面
	 * @return
	 */
	@RequestMapping("/showlist")
	public String showlist(ModelMap map, HttpServletRequest request){
		SysRole sysRole = sysRoleService.queryByRoleKey(getUserId(request));
		if (sysRole != null) {
			Criteria<GroupResource> params = new Criteria<GroupResource>();
			params.addParam("resourceIdList", getResourceId(sysRole.getResourceId()));
			params.addParam("parent_resource_id",new Long(request.getParameter("id")));
			params.addParam("status", 0);
			params.addParam("showType", 1);
			List<GroupResource> lists = groupResourceService.queryPageList(params);
            map.put("gpLists", JSONArray.toJSONString(lists));
		}
		map.put("id", request.getParameter("id"));
		map.put("classList", feedbackClassService.queryPageList());
		return "feedback/feedbackList";
	}
	
	/**
	 * 获取建议列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/querypage")
	@ResponseBody
	public PageResult queryPage(HttpServletRequest request){
		 Criteria<Feedback> param = new Criteria<Feedback>();
		 if (!StringUtil.isNullOrBlank(request.getParameter("type"))) {
			 param.addParam("type", request.getParameter("type"));
		 }
         if (!StringUtil.isNullOrBlank(request.getParameter("status"))) {
        	 param.addParam("status", request.getParameter("status"));
         }
 	     if (!StringUtil.isNullOrBlank(request.getParameter("pageNumber"))) {
	    	param.addParam("startIndex", Integer.parseInt(request.getParameter("pageNumber")));
	     }
	     if (!StringUtil.isNullOrBlank(request.getParameter("pageSize"))) {
	    	param.addParam("endPage", Integer.parseInt(request.getParameter("pageSize")));
	     }
	     List<Map<String,Object>> list = service.queryPage(param);
	     PageResult pageResult = new PageResult();
	     pageResult.setTotal(service.queryPageCount(param));
		 pageResult.setRows(list);
		 return pageResult;	
	}
	
	/**
	 * 
	 * @param id
	 * @param map
	 * @return
	 */
	@RequestMapping("/detail")
	public String detail(String id,String resourceId,String status,ModelMap map){
		map.put("resourceId", resourceId);
		map.put("status", status);
		Feedback feedback = service.queryEntity(new Long(id));
		if (feedback != null) {
			 if (!StringUtil.isNullOrBlank(feedback.getPicture())) {
				 String[] split = feedback.getPicture().split(",");
				 List<Feedback> list = new ArrayList<Feedback>();
				 if(split.length > 5){
					 for (int i = 0; i < 4; i++) {
						 Feedback f = new Feedback();
						 f.setPicture(split[i]);
						 list.add(f);
					 }
				 }else{
					 for (int i = 0; i < split.length; i++) {
						 Feedback f = new Feedback();
						 f.setPicture(split[i]);
						 list.add(f);
					 }
				 }
				 feedback.setPictureList(list);
			 }
		}
		map.put("feedback", feedback);
		return "feedback/feedbackDetail";
	}
	
	/**
	 * 跟根据id删除建议
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public JsonResult delete(String id) {
		JsonResult jr = JsonResult.newInstance();
		String ret = service.deleteAll(id);
	    String content = "删除了"+id+"建议";
	    insertLog(content);
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret);
	}


	/**
	 *跟根据id修改建议
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = { RequestMethod.POST })
	public JsonResult update(HttpServletRequest request) {
		JsonResult jr = JsonResult.newInstance();
		String content = "";
		Criteria<Feedback> param = new Criteria<Feedback>();
		Feedback back = new Feedback();
		if (!StringUtil.isNullOrBlank(request.getParameter("status"))) {
			back.setStatus(request.getParameter("status"));
			if (request.getParameter("status").equals(STR_1)) {
			    content ="被采纳"+request.getParameter("type")+"建议";
			}else {
				content ="未被采纳"+request.getParameter("type")+"建议";
			}
		}
	    back.setHandlingAt(new Date());
		if (!StringUtil.isNullOrBlank(request.getParameter("remark"))) {
			back.setRemark(request.getParameter("remark"));
			SendNotificationService sendNoti = new SendNotificationService();
			Feedback feedback = service.queryEntity(new Long(request.getParameter("id")));
			if (feedback != null) {
				Long userId = new Long(feedback.getUserId());
				sendNoti.sendToUser(userId, "11", "你的反馈已被处理!");
			}
		}
		back.setIsFeedback("1");
		param.setRecord(back);
		param.addParam("id", new Long(request.getParameter("id")));
		String ret = service.update(param);
		insertLog(content);
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret);
	}
}